<?php
class Core_Interface_Template
{
    protected $_forbiddenElements = array('id', 'name', 'label', 'headline');

    public function load($template)
    {
        if (is_object($template)) {
            return $template;
        } else if (file_exists($template)) {
            $form = new Core_Content_Form();
            $view = $form->getView();
            $templateName = basename($template);
            $layoutPath = Core_Toolbox_String::getParentFromPath($template);
            $view->addScriptPath($layoutPath);
            $fileContents = $view->render($templateName);
            $cleanFile = preg_replace('/(<\?{1}[pP\s]{1}.+\?>)/', '', $fileContents);
            return simplexml_load_string($cleanFile);
        } else {
            return simplexml_load_string($template);
        }
    }

    public function getControls($template)
    {
        if (($template = $this->load($template))) {
            return $template->xpath('//coreControl');
        }
        return false;
    }

    public function getForm($template, $formInstance = null, $content = null)
    {
        if ($formInstance == null) {
            $formInstance = new Core_Content_Form();
        }
        $view = $formInstance->getView();
        $controls = $this->getControls($template);
        if ($controls) {
            foreach ($controls as $control) {
                $attribs = $control->attributes();
                $id = (string)$attribs['id'];
                if (!in_array($id, $this->_forbiddenElements)) {
                    $type = (string)$attribs['type'];
                    if (isset($attribs['required'])) {
                        $required = true;
                        unset($attribs['required']);
                    } else {
                        $required = false;
                    }

                    if (isset($attribs['label'])) {
                        $label = $view->getTranslation((string)$attribs['label']);
                        unset($attribs['label']);
                    } else {
                        $label = $view->getTranslation(ucwords(str_replace('_', ' ', $id)));
                    }

                    $control = $formInstance->createElement($type, $id, array(
                        'label'         => $label,
                        'required'      => $required,
                        'decorators'    => array('ViewHelper')
                    ));
                    $control->setAttrib('rel', isset($attribs['group']) ? (string)$attribs['group'] : 'main');
                    foreach ($attribs as $attribute => $value) {
                        $control->setAttrib($attribute, $value);
                    }
                    if (isset($content[$id])) {
                        $control->setValue($content['id']);
                    }
                    $formInstance->addElement($control);

                    // set the display group
                    // $displayGroup = (isset($attribs['group']))? (string)$attribs['group'] : 'main';
                    // $formInstance->addDisplayGroup(array($control), $displayGroup);
                }
            }
        }
        return $formInstance;
    }

    public function getPageData($template, $page, $scope = 'public')
    {
        $config = Zend_Registry::get('config')->template;
        return $this->load(BASE_PATH . '/' . $config->pathToTemplates . '/' . $scope . '/' . $template . '/pages/' . $page . '.xml');
    }
}